<?php

class PagerHelper extends Helper
{

  public function show(Pager $pager, $params = null)
  {
    $output = '';

    if ($pager->getTotalPages() > 1) {
      $output .= '<ul class="amp-toolbar-item pager">';

      $route = s::request()->route();

      $get = array('limit' => $pager->getPageSize());

      if ($pager->isFirstOffset()) {
        $output .= '<li class="first disabled">'.$this->icon->show('first').'</li>';
        $output .= '<li class="previous disabled">'.$this->icon->show('previous').'</li>';
      }
      else {
        $get['offset'] = $pager->getFirstOffset();
        $output .= '<li class="first"><a href="' . s::response()->makeUrl($route, $get) . '">'.$this->icon->show('first').'</a></li>';

        $get['offset'] = $pager->getPreviousOffset();
        $output .= '<li class="previous"><a href="' . s::response()->makeUrl($route, $get) . '">'.$this->icon->show('previous').'</a></li>';
      }

      foreach ($pager->getPageList() as $page) {
        if ($pager->getCurrentPage() == $page) {
          $output .= '<li class="page current">' . $page . '</li>';
        }
        else {
          $get['offset'] = $pager->getOffsetFromPage($page);
          $output .= '<li class="page"><a href="' . s::response()->makeUrl($route, $get) . '">' . $page . '</a></li>';
        }
      }

      if ($pager->isLastOffset()) {
        $output .= '<li class="next disabled">'.$this->icon->show('next').'</li>';
        $output .= '<li class="last disabled">'.$this->icon->show('last').'</li>';
      }
      else {
        $get['offset'] = $pager->getNextOffset();
        $output .= '<li class="next"><a href="' . s::response()->makeUrl($route, $get) . '">'.$this->icon->show('next').'</a></li>';

        $get['offset'] = $pager->getLastOffset();
        $output .= '<li class="last"><a href="' . s::response()->makeUrl($route, $get) . '">'.$this->icon->show('last').'</a></li>';
      }

      $output .= '</ul>';
    }

    return $output;
  }

}

?>